Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Persist theme in localStorage #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iamdarshshah
Copy link
Contributor

@iamdarshshah iamdarshshah commented May 25, 2020

[ Reference #91 ]

Custom Hook for the theme change that will update the theme globally by adding it to the localStorage.

This will help us to persist the selected theme in local storage and use it every time the page is refreshed.

Let me know your thoughts. Thanks

Here is the code for custom React hook:

const useThemeState = (defaultValue, key) => {
  const [value, setValue] = React.useState(() => {
    const theme = window.localStorage.getItem(key)

    return theme !== null ? JSON.parse(theme) : defaultValue
  })

  React.useEffect(() => {
    window.localStorage.setItem(key, JSON.stringify(value))
  }, [key, value])

  return [value, setValue]
}
 

@vercel
Copy link

vercel bot commented May 25, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/sid/react-ui/cmyz07wbv
✅ Preview: https://react-ui-git-fork-iamdarshshah-feat-theme.sid.now.sh

@iamdarshshah
Copy link
Contributor Author

@siddharthkp & @rubenmoya Any updates or feedback on this?

@siddharthkp siddharthkp changed the base branch from master to main June 27, 2020 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant